<?php 
class PublicSurveyAdmin extends Module
{
	function PublicSurveyAdmin($row)
	{
		Module::Module($row);
		
		CGlobal::$website_title = AZNet::$page['title'];
		
		if(User::is_admin())
		{
			$cmd=Url::get('cmd','list');
			
			switch($cmd)
			{
				case 'delete':	$this->add_form(new DeleteSurveyForm());break;
				case 'edit':	$this->add_form(new EditSurveyForm());break;
				case 'add':		$this->add_form(new AddSurveyForm());break;
				case 'publish':
				case 'unpublish': 
					$this->add_form(new SubmitLink()); break;
				default:$this->add_form(new ListSurveyForm());break;
			}
		}
	}
}



class ListSurveyForm extends Form
{
	function ListSurveyForm()
	{
		Form::Form('ListSurveyForm');
	}
	
	function draw()
	{
		global $display;
		$display->add('skin_path',IMAGE_PATH);

		$txt='';
		$option1='';

		$sql="SELECT * FROM `public_survey` WHERE ";

		$search=Url::get('search');

		if ($search!='')
		{
			$condition="title LIKE '%$search%' OR question LIKE '%$search%'";
		}
		else
		{
			$condition=" 1";
		}

		$totalRows=DB::count("public_survey",$condition);
		$limit='';
		
		$display->add('pagging',AZPagging::paging_list($limit,$totalRows,40));
		
		$sql.= $condition.$limit;	
		
		$block_id	=	(int)Url::get('block_id',0);
		$href		=	str_replace(WEB_DIR,'',urldecode(Url::get('href')));
		
		$surveys	=	DB::fetch_all($sql);
		
		if ($surveys)
		{
			foreach ($surveys as &$survey)
			{
				if($block_id)
				{
					$survey["add_to_block"] = $href.($href!=''?'&':'?')."cmd=add_poll_to_block&poll_id={$survey['id']}&block_id=$block_id";
				}
				
				if($survey['view']==1)
				{
					$survey["url_view"] 	= Url::build_current(array("cmd"=>"unpublish","id"=>$survey['id']));
					$survey["view"] 	= '<img src="style/images/unexam.gif" title="Ẩn"/>';
					$survey["view_str"] = "Hiển thị";
				}
				else
				{
					$survey["url_view"] 	= Url::build_current(array("cmd"=>"publish","id"=>$survey['id']));
					$survey["view_str"] = "Đang ẩn";
					$survey["view"] 	= '<img src="style/images/exam.gif" title="Cho hiển thị"/>';
				}
				
				$survey["url_view_survey"] 	= Url::build('view_survey',array('id'=>$survey['id']));
				$survey["url_option"] 		= Url::build('admin_option',array('id_survey'=>$survey['id']));
				
				$question	=	$survey['question'].'<br><br>';

				$total_cout = DB::fetch('SELECT SUM(`count`) AS total_cout FROM `public_survey_opinion` WHERE id_survey='.$survey['id'],'total_cout',0);
				
				$options 	= DB::fetch_all('SELECT * FROM `public_survey_opinion` WHERE id_survey='.$survey['id']);
				
				foreach ($options as $option)
				{
					$question.="+ [ID:{$option['id']}] <b>{$option['option']}</b> <font color=red>({$option['count']}/{$total_cout} lượt chọn)</font><br>";
				}

				$survey["question"] 		= $question;
				
				if ($survey['type']==0)
				{
					$type_send="Nhiều lựa chọn";
				}
				else
				{
					$type_send="Một lựa chọn";
				}

				$survey["type_send"] 		= $type_send;
				$survey["time"] 		= date("d/m/Y H:i",$survey["time"]);
				
				$survey["time_m"] 		= date("d/m/Y H:i",$survey["time_m"]);
				
				$survey["edit"] 	= Url::build_current(array('id_survey'=>$survey['id'],'cmd'=>'edit'));
				$survey["delete"] 		= Url::build_current(array('id_survey'=>$survey['id'],'cmd'=>'delete'));
			}
		}

		$display->add('surveys',$surveys);
		
		$display->add('url_addnew',Url::build('admin_survey',array('cmd'=>'add')));
		$display->output('ListSurvey');
	}
}

class AddSurveyForm extends Form
{
	function __construct()
	{
		Form::__construct('add_new_survey');
	}

	function on_submit()
	{
		$title		=Url::get('txt_title');
		$question	=Url::get('txt_question');
		$type		=Url::get('type',0);
		
		
		$this->checkFormInput('Tiêu đề','txt_title',	$title,		'str',	true,'',2,100);
		$this->checkFormInput('Câu hỏi','txt_question',	$question,	'str',	true,'',2,255);
		
		if(!$this->errNum)
		{
			$id_survey	=	DB::insert('public_survey',array(
														'title'			=>addslashes($title),
														'question'		=>addslashes($question),
														'type'			=>$type,
														'user_name'		=>User::user_name(),
														'time'			=>TIME_NOW,
														'm_user_name'	=>User::user_name(),
														'time_m'		=>TIME_NOW
														));
			
			if($id_survey)
			{
				Url::redirect('admin_option',array('id_survey'=>$id_survey));
			}
			else
				$this->setFormError('txt_question',"Không thể thêm mới, hãy thử lại!");
		}
	}

	function draw()
	{
		global $display;
		$this->beginForm();
		
		$display->add('msg',			$this->showFormErrorMessages(1));
		
		$display->add('txt_title',		stripslashes(Url::get('txt_title')));
		$display->add('type_option',	AZLib::getOption(array(0=>"Nhiều lựa chọn",1=>"Một lựa chọn"),Url::get('type',0)));
		$display->add('txt_question',	stripslashes(Url::get('txt_question')));
		$display->add('url_admin',		Url::build('admin_survey'));
		
		$display->output('AddSurvey');
		$this->endForm();
	}
}

class EditSurveyForm extends Form
{
	var $public_survey = false;
	function __construct()
	{
		Form::__construct('edit_survey');
		
		$id_survey	= (int)Url::get('id_survey',0);
		
		if($id_survey)
		{
			$this->public_survey = DB::select('public_survey', "id=$id_survey");
			
			if (!$this->public_survey)
			Url::redirect_current();
		}
	}

	function on_submit()
	{
		global $oDB;
		
		$title		=Url::get('txt_title');
		$question	=Url::get('txt_question');
		$type		=Url::get('type',0);
		
		
		$this->checkFormInput('Tiêu đề','txt_title',	$title,		'str',	true,'',2,100);
		$this->checkFormInput('Câu hỏi','txt_question',	$question,	'str',	true,'',2,255);
		
		if(!$this->errNum)
		{
			$result=DB::update('public_survey',array(
														'title'			=>addslashes($title),
														'question'		=>addslashes($question),
														'type'			=>$type,
														'm_user_name'	=>User::user_name(),
														'time_m'		=>TIME_NOW
														)
														,'id='.$this->public_survey['id']);
	
			if($result)
			{
				Url::redirect_current();
			}
			else 
			{
				$this->setFormError('txt_question',"Không cập nhật được!");
			}
		}
	}

	function draw()
	{
		global $display;
		$this->beginForm();
		
		$display->add('msg',			$this->showFormErrorMessages(1));
		
		$display->add('txt_title',		stripslashes(Url::get('txt_title',$this->public_survey['title'])));
		$display->add('type_option',	AZLib::getOption(array(0=>"Nhiều lựa chọn",1=>"Một lựa chọn"),Url::get('type',$this->public_survey['type'])));
		$display->add('txt_question',	stripslashes(Url::get('txt_question',$this->public_survey['question'])));
		$display->add('url_admin',		Url::build('admin_survey'));
		
		$display->output('EditSurvey');
		$this->endForm();
	}
}

class DeleteSurveyForm extends Form
{
	function DeleteSurveyForm()
	{
		Form::__construct('DeleteSurveyForm');
		
		$id_survey	=	(int)Url::get('id_survey',0);
		
		if($id_survey)
		{
			DB::delete('public_survey','id='.$id_survey);
			DB::delete('public_survey_opinion','id_survey='.$id_survey);
		}
		
		Url::redirect_current();
	}
}

class SubmitLink extends Form
{
	function SubmitLink()
	{
		$id = (int)Url::get('id',0);
		
		if ($id)
		{
			$cmd=Url::get('cmd');
			
			if ($cmd=='publish')
			{
				DB::update('public_survey', array('view'=>'1'),'id='.$id);
				DB::update('public_survey', array('view'=>'0'),'id!='.$id);
			}
			else
			{
				DB::update('public_survey', array('view'=>'0'),'id='.$id);
				DB::update('public_survey', array('view'=>'1'),'id!='.$id);
			}
		}
		
		Url::redirect_current();
	}
}
?>